package com.yorick.login.controller;

import com.yorick.common.Result.SimpleFactory.ResultFactory;
import com.yorick.common.dao.SecurityMapper;
import com.yorick.common.error.YorickAssert;
import com.yorick.login.modal.request.LoginRequest;
import com.yorick.login.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.session.SessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;
import java.util.Random;

/**
 * @program: LoginController
 * @description: 登录模块
 * @author: Yorick
 * @create: 29/5/2022
**/
@Slf4j
@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private LoginService loginService;

    @Value("${yorick.enableCode:true}")
    private boolean enableCode;

    @RequestMapping(value = "getCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCode(HttpServletRequest request) {
        HttpSession session = request.getSession();
        //在Java中怎么把1到9999的数字转成 4位字符串,左边补0 如 0001 0002 0003
        String var = new String();
        for (int j = 0; j < 4; j++) {
            Random r = new Random();
            if (r.nextInt(2) < 1) {
                String chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
                var = var + chars.charAt((int) (Math.random() * 25));
            } else {
                String chars = "123456789";
                var = var + chars.charAt((int) (Math.random() * 9));
            }

        }
        log.info(var);
        session.setAttribute("loginCode", var);
        return ResultFactory.success(var).display();
    }

    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> doLogin(@Valid @RequestBody LoginRequest requestBody, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (enableCode) {
            YorickAssert.validate(
                    StringUtils.equalsIgnoreCase(
                            session.getAttribute("loginCode").toString(),
                            requestBody.getCode()
                    ),
                    () -> "0",
                    () -> "验证码错误"
            );
        }
        return loginService.handleLogin(requestBody, request);

    }

    @RequestMapping(value = "doLogout", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> doLogout(HttpSession session, SessionStatus sessionStatus) {
        try {
            session.invalidate();
            sessionStatus.setComplete();
            return ResultFactory.success().display();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultFactory.failure().display();
        }
    }
}
